package com.itheima.wemedia.controller;

import com.itheima.admin.pojo.AdChannel;
import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import com.itheima.wemedia.pojo.WmMaterial;
import com.itheima.wemedia.pojo.WmNews;
import com.itheima.wemedia.pojo.WmUser;
import com.itheima.wemedia.dto.AuthReqDto;
import com.itheima.wemedia.dto.WmNewsPageReqDto;
import com.itheima.wemedia.service.ApiService;
import com.itheima.wemedia.service.WmMaterialService;
import com.itheima.wemedia.service.WmNewsService;
import com.itheima.wemedia.service.WmUserService;
import com.itheima.wemedia.vo.WmNewsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Cheneg
 * @version 1.0
 */

@RestController
public class ApiController {

    @Autowired
    private WmUserService wmUserService;

    @Autowired
    private WmMaterialService wmMaterialService;
    @Autowired
    private WmNewsService wmNewsService;

    @Autowired
    private ApiService apiService;

    /**
     * 远程调用添加自媒体账号
     *
     * @param wmUser
     * @return
     */
    @PostMapping("api/wmUser/add")
    public ResultVo<WmUser> add(@RequestBody WmUser wmUser) {
        wmUserService.save(wmUser);
        return ResultVo.ok(wmUser);
    }

    /**
     * 查询自媒体账号信息
     */
    @GetMapping("api/wmUser/{addUserId}")
    public ResultVo<WmUser> getByAppUserId(@PathVariable(name = "appUserId") Integer appUserId) {
        WmUser wmUser = wmUserService.getByAppUserId(appUserId);
        return ResultVo.ok(wmUser);

    }


    /**
     * 添加素材
     *
     * @param wmMaterial
     * @return
     */
    @PostMapping("/api/wmMaterial/add")
    public ResultVo add(@RequestBody WmMaterial wmMaterial) {
        if (wmMaterialService.save(wmMaterial)){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 查询所有的频道
     * @return
     */
    @GetMapping("/channel/channels")
    public ResultVo<List<AdChannel>> channelList(){
        List<AdChannel> channelList = apiService.getChannelList();
        return ResultVo.ok(channelList);
    }

    /**
     * 通过id查询文章信息
     * @return
     */
    @GetMapping("/api/wmNews/{newsId}")
    public ResultVo<WmNews> getWmNewsById(@PathVariable(name = "newsId") Integer newsId){
        WmNews wmNews = wmNewsService.getById(newsId);
        return ResultVo.ok(wmNews);
    }

    /**
     * 通过id更新文章信息
     * @return
     */
    @PutMapping("/api/wmNews/update")
    public ResultVo<WmNews> update(@RequestBody WmNews wmNews){
        wmNewsService.updateById(wmNews);
        return ResultVo.ok();
    }

    /**
     * 条件分页查询
     * @param dto
     * @return
     */
    @PostMapping("/news/list_vo")
    public PageResultVo<WmNewsVo> findPage4Admin(@RequestBody WmNewsPageReqDto dto){
        PageResultVo<WmNewsVo> pageResultVo = wmNewsService.findPage4Admin(dto);
        return pageResultVo;
    }


    /**
     * 人工审核 - 通过
     * @param dto
     * @return
     */
    @PostMapping("/news/auth_pass")
    public ResultVo authPass(@RequestBody @Validated AuthReqDto dto){
        wmNewsService.authPass(dto);
        return ResultVo.ok();
    }

    /**
     * 人工审核 - 驳回
     * @param dto
     * @return
     */
    @PostMapping("/news/auth_fail")
    public ResultVo authFail(@RequestBody AuthReqDto dto){
        wmNewsService.authFail(dto);
        return ResultVo.ok();
    }

    /**
     * 查看文章详情
     * @param newsId
     * @return
     */
    @GetMapping("/news/one_vo/{newsId}")
    public ResultVo oneVo(@PathVariable Long newsId){
        WmNewsVo vo = wmNewsService.getOneVo(newsId);
        return ResultVo.ok(vo);
    }

    /**
     * 查询审核通过的文章，状态为审核通过4/8。进行数据同步到文章微服务
     * @param dto
     * @return
     */
    @PostMapping("/api/wmNews/list4ArticleSync")
    public ResultVo<List<WmNewsVo>> list4ArticleSync(@RequestBody WmNewsPageReqDto dto){
        List<WmNewsVo> newsVoList = wmNewsService.list4ArticleSync(dto);
        return ResultVo.ok(newsVoList);
    }
}
